package com.warrior.superdata.controller;

import com.warrior.superdata.annotation.MysqlCheckInject;
import com.warrior.superdata.base.BasePageResultVo;
import com.warrior.superdata.base.CommonResponse;
import com.warrior.superdata.base.CommonResult;
import com.warrior.superdata.constants.CommonConstant;
import com.warrior.superdata.mysql.entity.dto.AddApiParamDto;
import com.warrior.superdata.mysql.entity.dto.AddApiResultDto;
import com.warrior.superdata.mysql.service.ApiInfoService;
import com.warrior.superdata.mysql.entity.dto.ApiInfoDetailDto;
import com.warrior.superdata.mysql.entity.dto.ApiInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description:
 * @ClassName: ApiInfoController.java
 * @date: 2022/3/3 11:03
 * @author xujian
 */
@ApiSort(3)
@Api(tags = "api相关接口")
@RestController
@RequestMapping("/api/info/")
public class ApiInfoController {

    @Autowired
    private ApiInfoService apiInfoService;

    @ApiOperation(position = 1, value = "新增接口")
    @PostMapping("")
    @ApiImplicitParam(name = "apiInfoDetailDto", value = "接口实体", dataType = "ApiInfoDetailDto")
    @MysqlCheckInject
    public CommonResult addApiInfo(@Valid @RequestBody ApiInfoDetailDto apiInfoDetailDto) {
        apiInfoService.addApiInfo(apiInfoDetailDto);
        return CommonResponse.success(CommonConstant.CONSTANT_INSERT_SUCCESS);
    }

    @ApiOperation(position = 2, value = "更新接口")
    @PutMapping("")
    @ApiImplicitParam(name = "apiInfoDetailDto", value = "接口实体", dataType = "ApiInfoDetailDto")
    @MysqlCheckInject(method = "update")
    public CommonResult updateApiInfo(@RequestBody ApiInfoDetailDto apiInfoDetailDto) {
        apiInfoService.updateApiInfo(apiInfoDetailDto);
        return CommonResponse.success(CommonConstant.CONSTANT_UPDATE_SUCCESS);
    }

    @ApiOperation(position = 3, value = "删除接口")
    @DeleteMapping("")
    public CommonResult delete(Integer id) {
        apiInfoService.delete(id);
        return CommonResponse.success(CommonConstant.CONSTANT_DELETE_SUCCESS);
    }

    @ApiOperation(position = 4, value = "新增接口参数")
    @PostMapping("/param")
    @ApiImplicitParam(name = "addApiParamDto", value = "接口实体", dataType = "AddApiParamDto")
    public CommonResult addApiParam(@Valid @RequestBody AddApiParamDto addApiParamDto) {
        apiInfoService.addApiParam(addApiParamDto);
        return CommonResponse.success(CommonConstant.CONSTANT_INSERT_SUCCESS);
    }

    @ApiOperation(position = 5, value = "删除接口参数")
    @DeleteMapping("/param")
    @ApiImplicitParam(name = "addApiParamDto", value = "接口实体", dataType = "AddApiParamDto")
    public CommonResult deleteApiParam(@Valid @RequestBody AddApiParamDto addApiParamDto) {
        apiInfoService.deleteApiParam(addApiParamDto);
        return CommonResponse.success(CommonConstant.CONSTANT_DELETE_SUCCESS);
    }

    @ApiOperation(position = 6, value = "新增接口返回")
    @PostMapping("/result")
    @ApiImplicitParam(name = "addApiResultDto", value = "接口实体", dataType = "AddApiResultDto")
    public CommonResult addApiResult(@Valid @RequestBody AddApiResultDto addApiResultDto) {
        apiInfoService.addApiResult(addApiResultDto);
        return CommonResponse.success(CommonConstant.CONSTANT_INSERT_SUCCESS);
    }

    @ApiOperation(position = 7, value = "删除接口返回")
    @DeleteMapping("/result")
    @ApiImplicitParam(name = "addApiResultDto", value = "接口实体", dataType = "AddApiResultDto")
    public CommonResult deleteApiResutl(@RequestBody AddApiResultDto addApiResultDto) {
        apiInfoService.deleteApiResutl(addApiResultDto);
        return CommonResponse.success(CommonConstant.CONSTANT_DELETE_SUCCESS);
    }

    @ApiOperation(position = 8, value = "接口分页查询")
    @PostMapping("/page")
    @ApiImplicitParam(name = "apiInfoDto", value = "接口实体", dataType = "ApiInfoDto")
    public CommonResult<BasePageResultVo<List<ApiInfoDetailDto>>> getApiInfoPage(@RequestBody ApiInfoDto apiInfoDto) {
        return CommonResponse.success(apiInfoService.getApiInfoPage(apiInfoDto));
    }

    @ApiOperation(position = 9, value = "新增或者更新接口")
    @PutMapping("save-or-update")
    @ApiImplicitParam(name = "apiInfoDetailDto", value = "接口实体", dataType = "ApiInfoDetailDto")
    @MysqlCheckInject(method = "update")
    public CommonResult saveOrUpdateApiInfo(@RequestBody ApiInfoDetailDto apiInfoDetailDto) {
        apiInfoService.saveOrUpdateApiInfo(apiInfoDetailDto);
        return CommonResponse.success(CommonConstant.CONSTANT_UPDATE_SUCCESS);
    }

}
